package com.sdtv.basemodule.utils;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.LogUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.utils
 * @ClassName: CommonUtils
 * @Description: 工具类--公共方法
 * @Author: 小郭同学
 * @CreateDate: 2019-07-16 10:24
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CommonUtils {


    /**
     * 视频时长格式化 --：--：--
     *
     * @param timeMs 时长
     * @return
     */
    public static String stringForTime(long timeMs) {
        StringBuilder mFormatBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        long totalSeconds = timeMs / 1000;
        long seconds = totalSeconds % 60;
        long minutes = (totalSeconds / 60) % 60;
        long hours = totalSeconds / 3600;
        mFormatBuilder.setLength(0);
        String time = "";
        if (hours > 0) {
            time = mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            time = mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
        return time;
    }



    /**
     * 获取指定文件大小(单位：字节)
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static long getFileSize(File file) throws Exception {
        if (file == null) {
            return 0;
        }
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        }
        return size;
    }


    //字节大小，K,M,G
    public static final long KB = 1024;
    public static final long MB = KB * 1024;
    public static final long GB = MB * 1024;

    /**
     * 文件字节大小显示成M,G和K
     *
     * @param size
     * @return
     */
    public static String displayFileSize(long size) {
        if (size >= GB) {
            return String.format("%.1fGB", (float) size / GB);
        } else if (size >= MB) {
            float value = (float) size / MB;
            return String.format(value > 100 ? "%.0fMB" : "%.1fMB", value);
        } else if (size >= KB) {
            float value = (float) size / KB;
            return String.format(value > 100 ? "%.0fKB" : "%.1fKB", value);
        } else {
            return String.format("%dB", size);
        }
    }


    /**
     * 判断网络状态
     *
     * @param mContext
     * @return
     */
    public static boolean isNetOk(Context mContext) {
        if (null != mContext) {
            ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager
                    .getActiveNetworkInfo();
            return activeNetInfo != null;
        }

        return true;
    }

    /**
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     *
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String md5Prestr(Map<String, String> params, long timestamp) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        String prestr = "";

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            try {
                if (!TextUtils.isEmpty(value)) {
                    value = URLDecoder.decode(value, "UTF-8");
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            prestr = prestr + key + value;
        }
        prestr = "qingkintegrate" + prestr + "_timestamp" + timestamp + "qingkintegrate";
        return EncryptUtils.encryptMD5ToString(prestr).toLowerCase();
    }

    public static Boolean isEmpty(String inputString) {
        if ("null".equals(inputString) || inputString == null || (inputString != null && inputString.trim().length() == 0)) {
            return true;
        }
        return false;
    }


}
